package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.ContentVo;
import com.baike.entity.TbContent;
import com.baike.service.ITbContentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author x_FG
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/tbContent")
@Api(value = "广告详情管理接口")
@RefreshScope
public class TbContentController {

    @Autowired
    private ITbContentService contentService;

    @ApiOperation("广告详情查询方法")
    @GetMapping("findAll")
    public Result getContentInfo(){
        List<TbContent> list = contentService.list();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }
    // 条件查询-分栏,时间(前台展示)
    @ApiOperation("前台首页展示广告调用方法")
    @GetMapping("front")
    public Result getFrontInfo(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        ContentVo contentVo = contentService.getFrontInfo(format);
        return Result.build(ConstantEnum.SUCCESS).setResult(contentVo);
    }
    // 广告文件上传查询某个时段是否存在(教师投放)
    @ApiOperation("广告文件上传查询某个时段是否存在方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "tb_content_category实体类主键id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "createTime",value = "开始时间",required = true,paramType = "query"),
            @ApiImplicitParam(name = "updateTime",value = "结束时间",required = true,paramType = "query")
    })
    @GetMapping("time")
    public Result getThinkTimeInfo(@RequestParam("id")Integer id,
            @RequestParam("createTime")Date createTime,@RequestParam("updateTime")Date updateTime){

        // 1. 查询出表中时间段存在最近的当前时间
        boolean b = contentService.getThinkTimeInfo(id,createTime,updateTime);
        if (b){
            return Result.build(ConstantEnum.SUCCESS).setResult("该时间段可以能投入广告-->ok");
        }
        return Result.build(ConstantEnum.ERROR).setResult("该时间段不能够投入广告-->NO");
    }

    @ApiOperation("增加上传广告方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "栏位",required = true,paramType = "query"),
            @ApiImplicitParam(name = "title",value = "内容标题",required = true,paramType = "query"),
            @ApiImplicitParam(name = "sortOrder",value = "排序",required = true,paramType = "query"),
            @ApiImplicitParam(name = "url",value = "连接",required = true,paramType = "query"),
            @ApiImplicitParam(name = "cimage",value = "图片绝对路径",required = true,paramType = "query"),
            @ApiImplicitParam(name = "createTime",value = "开始时间",required = true,paramType = "query"),
            @ApiImplicitParam(name = "updateTime",value = "结束时间",required = true,paramType = "query"),
    })
    @PostMapping("/add")
    public Result insertContentInfo(@RequestBody TbContent tbContent){
//        Map<String,String> map = fileUpload(file);
//        tbContent.setUrl(map.get("fileUrl").toString());
//        tbContent.setCimage(map.get("fileUrl").toString());
        boolean b = contentService.insertContentInfo(tbContent);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

//
//    public Map<String,String> fileUpload(@RequestParam(value = "file") MultipartFile[] file){
//        Map<String,String> map = new HashMap();
//        for (MultipartFile multipartFile : file) {
//            try {
//                String url = contentService.fileUpload(multipartFile.getInputStream());
//                map.put("fileUrl",url);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return map;
//    }



    @ApiOperation("广告详情表修改方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "TbContent",value = "TbContent实体类",required = true,paramType = "body")
    })
    @PutMapping
    public Result updateContentInfo(@RequestBody TbContent tbContent){
        boolean b = contentService.updateContentInfo(tbContent);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }


    @ApiOperation("广告详情的审核/下架调用方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "TbContent实体类主键Id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "status",value = "要修改的状态",required = true,paramType = "path")
    })
    @PutMapping("status/{id}/{status}")
    public Result updateStatusInfo(@PathVariable("id")Integer id,@PathVariable("status")Integer status){
        boolean b = contentService.updateStatusInfo(id,status);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }


    @ApiOperation("广告详情批量/单个删除方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "TbContent实体类主键Id",required = true,paramType = "path")
    })
    @DeleteMapping("/{id}")
    public Result deleteContentInfo(@PathVariable("id")Integer[] id){
        boolean b = contentService.removeByIds(Arrays.asList(id));
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }


}
